/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var findBottomLeftValue = function(root) {
    let queue = [root],
    res,
    node,
    len;
    while(queue.length > 0){
        len = queue.length
        res = queue[0].val

        for(let i = 0;i<len;i++){
            node = queue.shift()
            node.left && queue.push(node.left)
            node.right && queue.push(node.right)
        }

    }
    return res
};


/**
 * @param {TreeNode} root
 * @return {number}
 */
var findBottomLeftValue = function (root) {
    let node,maxDepth=0
    function searchLevelLeftValue(root, curDepth) {
        curDepth++
        if (root.left === null && root.right === null) {
            if (curDepth > maxDepth) {
                node = root
                maxDepth = curDepth
            }
            return node
        }

        root.left && searchLevelLeftValue(root.left, curDepth)
        root.right && searchLevelLeftValue(root.right, curDepth)
        return
    }
    searchLevelLeftValue(root, 0)
    return node.val
};